/**
 * @author wangning 20180414
 * 基本情况
 */
import { Component, OnInit, OnChanges } from "@angular/core";
import { HttpServcie } from "../../../../service/http.service";
import {NzNotificationService} from "ng-zorro-antd";

@Component({
  selector: "basic-information",
  templateUrl: "./basic-information.html",
  styleUrls: ["./basic-information.scss"]
})
export class BasicInformation implements OnInit {
  option1 = {}; //辖区区电梯数量分布情况
  option2 = {}; //辖区电梯类型分布图
  option3 = {}; //电梯梯龄情况
  option4 = {}; //辖区电梯安全保障力量分布情况-持证人员

  option6 = {};
  public elevatorNum: number; //电梯总数
  public elevatorNumStyle:any; // 电梯总数样式数据

  public brandNum: number; //电梯品牌数量
  public brandNumStyle: any; //电梯品牌数量样式数据

  public operatorNum: number; //特种设备作业人员总数
  public operatorNumStyle: any; //特种设备作业人员总数样式数据

  public usedPartyNum: number; //使用单位数
  public usedPartyNumStyle:any;// 使用单位数样式数据

  public maintPartyNum: number; //维保单位数
  public maintPartyNumStyle: any; //维保单位数

  public contractNum: number; //项目/小区总数
  public contractNumStyle: number; //项目/小区总数


  importantElevatorCount: number;
  unimportantElevatorCount: number;





  partyId: any = JSON.parse(window.localStorage.getItem("user")).partyId;
  // 单位等级
  partyLevel: any = JSON.parse(window.localStorage.getItem("user")).partyLevel;

  partyParentId:any =  JSON.parse(window.localStorage.getItem("user")).partyParentId;



  distribRet = [];

  constructor(private httpServcie: HttpServcie,
              private _notification: NzNotificationService) { }

  ngOnInit() {
    //辖区内电梯年限分布
    this.countYearDistrib();
    //辖区内保障力量分布
    this.countOperDistrib();
    //辖区内电梯各项统计
    this.countEle();
    //辖区内电梯类型分布
    this.countTypeDistrib();
    //辖区内电梯数量分布
    this.countNumDistrib();

  }

  //辖区内电梯年限分布
  countYearDistrib() {
    if (this.partyLevel == null || this.partyLevel == undefined) {
      this._notification.create("warning", "提示", "等级不能为空，请联系超管");
      return;
    }

    if (this.partyLevel == 3) {
      this.partyId = this.partyParentId;
    }

    this.httpServcie.countYearDistrib({"id":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {
      if (_data.data) {
        let ret = _data.data;
        this.option3 = {
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          toolbox: {
            show : true,
            feature : {
              mark : {show: true},
              dataView : {show: true, readOnly: false},
              magicType : {
                show: true,
                type: ['pie', 'funnel'],
                option: {
                  funnel: {
                    x: '25%',
                    width: '50%',
                    funnelAlign: 'left',
                    max: 1548
                  }
                }
              },
              restore : {show: true},
              saveAsImage : {show: true}
            }
          },
          legend: {
            orient: "vertical",
            left: "left",
            data: ["10年以下", "10年-15年", "15年-20年", "20年以上"]
          },
          color:['#D87A80', '#2EC7C9','#B6A2DE','#FFB980'],
          series: [
            {
              name: "访问来源",
              type: "pie",
              radius: "55%",
              center: ["50%", "60%"],
              data: [
                { value: ret.lessTen, name: "10年以下" },
                { value: ret.lessFifteen, name: "10年-15年" },
                { value: ret.lessTwenty, name: "15年-20年"},
                { value: ret.gatherTwenty, name: "20年以上"}
              ],
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: "rgba(0, 0, 0, 0.5)"
                }
              }
            }
          ]
        };
      }
    });
  }

  //辖区内保障力量分布
  countOperDistrib() {

    if (this.partyLevel == null || this.partyLevel == undefined) {
      this._notification.create("warning", "提示", "等级不能为空，请联系超管");
      return;
    }

    if (this.partyLevel == 3) {
      this.partyId = this.partyParentId;
    }

    this.httpServcie.countOperDistrib({"id":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {
      if (_data.data) {
        let ret = _data.data;
        this.option4 = {
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          toolbox: {
            show : true,
            feature : {
              mark : {show: true},
              dataView : {show: true, readOnly: false},
              magicType : {
                show: true,
                type: ['pie', 'funnel'],
                option: {
                  funnel: {
                    x: '25%',
                    width: '50%',
                    funnelAlign: 'left',
                    max: 1548
                  }
                }
              },
              restore : {show: true},
              saveAsImage : {show: true}
            }
          },
          legend: {
            orient: "vertical",
            left: "left",
            data: ["特种设备安全监察人员", "电梯安全管理人员", "电梯作业人员"]
          },
          color:['#D87A80', '#2EC7C9','#FFB980'],
          series: [
            {
              name: "访问来源",
              type: "pie",
              radius: "55%",
              center: ["50%", "60%"],
              data: [
                { value: ret.watcherNum, name: "特种设备安全监察人员" },
                { value: ret.saferNum, name: "电梯安全管理人员" },
                { value: ret.mainterNum, name: "电梯作业人员" }
              ],
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: "rgba(0, 0, 0, 0.5)"
                }
              }
            }
          ]
        };
      }
    });
  }

  //辖区内电梯各项统计
  countEle() {

    if (this.partyLevel == null || this.partyLevel == undefined) {
      this._notification.create("warning", "提示", "等级不能为空，请联系超管");
      return;
    }

    if (this.partyLevel == 3) {
      this.partyId = this.partyParentId;
    }
    this.httpServcie.countEle({"partyId":this.partyId,"partyLevel":this.partyLevel}).subscribe(data => {
      if (data.data) {
        let ret = data.data;
        this.elevatorNum = ret.elevatorNum;
        // 处理样式数据
        this.elevatorNumStyle = ret.elevatorNum * 0.4;

        this.brandNum = ret.brandNum;
        this.brandNumStyle = ret.brandNum * 0.3;

        this.operatorNum = ret.operatorNum;
        this.operatorNumStyle = ret.operatorNum * 0.2;

        this.usedPartyNum = ret.usedPartyNum;
        this.usedPartyNumStyle = ret.usedPartyNum * 0.2;

        this.maintPartyNum = ret.maintPartyNum;
        this.maintPartyNumStyle = ret.maintPartyNum * 0.4;

        this.contractNum = ret.contractNum;
        this.contractNumStyle = ret.contractNum * 0.2;


        this.importantElevatorCount = ret.importantElevatorCount;
        this.unimportantElevatorCount = ret.unimportantElevatorCount;

        var radius = [40, 55];

        var labelFromatter = {
          normal : {
            label : {
              formatter : function (params){
                return params.value
              },
              textStyle: {
                color:'#000000',
                baseline : 'top'
              }
            }
          },
        }

        var labelBottom = {
          normal : {
            color: '#C9C9C7',
            label : {
              show : true,
              position : 'center'
            },
            labelLine : {
              show : false
            }
          },
          emphasis: {
            color: 'rgba(0,0,0,0)'
          }
        };

        var labelTop = {
          normal : {
            label : {
              show : true,
              position : 'center',
              formatter : '{b}',
              textStyle: {
                baseline : 'bottom'
              }
            },
            labelLine : {
              show : false
            }
          }
        };

        this.option6 = {
          color:['#D87A80','#2EC7C9','#B6A2DE','#5AB1EF','#FFB980','#DC69AA'],
          series : [
            {
              type : 'pie',
              center : ['10%', '20%'],
              radius : radius,
              x: '0%', // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.elevatorNum, itemStyle : labelBottom},
                {name:'电梯总数', value:this.elevatorNumStyle,itemStyle : labelTop}
              ]
            },
            {
              type : 'pie',
              center : ['27%', '20%'],
              radius : radius,
              x:'20%', // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.brandNum, itemStyle : labelBottom},
                {name:'品牌数量', value:this.brandNumStyle,itemStyle : labelTop}
              ]
            },
            {
              type : 'pie',
              center : ['42%', '20%'],
              radius : radius,
              x:'40%', // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.operatorNum, itemStyle : labelBottom},
                {name:'作业人员总数', value:this.operatorNumStyle,itemStyle : labelTop}
              ]
            },
            {
              type : 'pie',
              center : ['57%', '20%'],
              radius : radius,
              x:'60%', // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.usedPartyNum, itemStyle : labelBottom},
                {name:'使用单位数', value:this.usedPartyNumStyle,itemStyle : labelTop}
              ]
            },
            {
              type : 'pie',
              center : ['72%', '20%'],
              radius : radius,
              x:'80%', // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.maintPartyNum, itemStyle : labelBottom},
                {name:'维保单位数', value:this.maintPartyNumStyle,itemStyle : labelTop}
              ]
            },
            {
              type : 'pie',
              center : ['87%', '20%'],
              radius : radius,
              y: '55%',   // for funnel
              x: '0%',    // for funnel
              itemStyle : labelFromatter,
              data : [
                {name:'other', value:this.contractNum, itemStyle : labelBottom},
                {name:'项目/小区数', value:this.contractNumStyle,itemStyle : labelTop}
              ]
            }
          ]
        };


      }
    });
  }

  //辖区内电梯类型分布
  countTypeDistrib() {

    if (this.partyLevel == null || this.partyLevel == undefined) {
      this._notification.create("warning", "提示", "等级不能为空，请联系超管");
      return;
    }
    if (this.partyLevel == 3) {
      this.partyId = this.partyParentId;
    }

    this.httpServcie.countTypeDistribType({"id":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {
      if (_data.data) {
        let ret = _data.data;
        if (ret.length > 0) {
          let typeName=[];
          let helpValue=[];
          let typeCount=[];

          let totalNum:number=0;
          for (let i of ret) {
            typeName.push(i.name);
            helpValue.push(i.value);
            typeCount.push(i.count);

            totalNum +=i.count;

          }
          typeName.unshift("总电梯数");
          helpValue.unshift(0);
          typeCount.unshift(totalNum);

          this.option2 = {
            tooltip : {
              trigger: 'axis',
              axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
              },
              formatter: function (params) {
                var tar = params[1];
                return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
              }
            },
            toolbox: {
              show : true,
              feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                restore : {show: true},
                saveAsImage : {show: true}
              }
            },
            color:['#D87A80'],
            xAxis : [
              {
                type : 'category',
                splitLine: {show:false},
                data : typeName
              }
            ],
            yAxis : [
              {
                type : 'value'
              }
            ],
            series : [
              {
                name:'其他',
                type:'bar',
                stack: '总量',
                itemStyle:{
                  normal:{
                    barBorderColor:'rgba(0,0,0,0)',
                    color:'rgba(0,0,0,0)'
                  },
                  emphasis:{
                    barBorderColor:'rgba(0,0,0,0)',
                    color:'rgba(0,0,0,0)'
                  }
                },
                data:helpValue
              },
              {
                name:'数量',
                type:'bar',
                stack: '总量',
                itemStyle : { normal: {label : {show: true, position: 'inside'}}},
                data:typeCount
              }
            ]
          };
        }
      }
    });
  }

  //辖区内电梯数量分布
  countNumDistrib() {

    if (this.partyLevel == null || this.partyLevel == undefined) {
      this._notification.create("warning", "提示", "等级不能为空，请联系超管");
      return;
    }

    if (this.partyLevel == 3) {
      this.partyId = this.partyParentId;
    }
    this.httpServcie.countNumDistrib({"partyId":this.partyId,"partyLevel":this.partyLevel}).subscribe(_data => {

      if (_data.data) {
        let ret = _data.data;
        this.distribRet = JSON.parse(JSON.stringify(ret));
        this.distribRet.unshift({
          eleStopNum: "停用电梯数",
          eleUsedNum: "在用电梯数",
          elevatorNum: "电梯总数",
          partyName: "\\"
        });
        let partyNameArr = [];
        let eleStopNumArr = [];
        let eleUsedNumArr = [];

        // 地区名称简称
        let partyNameShorthand=[];

        for (let i of ret) {
          partyNameArr.push(i.partyName);
          eleStopNumArr.push(i.eleStopNum);
          eleUsedNumArr.push(i.eleUsedNum);
          if(i.newPartyName !=undefined && i.newPartyName !=null){
            partyNameShorthand.push(i.newPartyName);
          }
        }
        if(partyNameShorthand.length ==0){
          partyNameShorthand = partyNameArr;
        }
        this.option1 = {
          tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
              type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          toolbox: {
            show : true,
            feature : {
              dataView : {show: true, readOnly: false},
              magicType : {show: true, type: ['line', 'bar']},
              restore : {show: true},
              saveAsImage : {show: true}
            }
          },
          legend: {
            data:['在用电梯数','停用电梯数']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis : [
            {
              type : 'category',
              data : partyNameShorthand
            }
          ],
          yAxis : [
            {
              type : 'value'
            }
          ],
          series : [
            {
              name:'在用电梯数',
              type:'bar',
              stack: '电梯',
              itemStyle : {
                normal : {
                  color:'#2EC7C9',  //圈圈的颜色
                  lineStyle:{
                    color:'#2EC7C9'  //线的颜色
                  }
                }
              },
              data:eleUsedNumArr
            },
            {
              name:'停用电梯数',
              type:'bar',
              stack: '电梯',
              itemStyle : {
                normal : {
                  color:'#D87A80',  //圈圈的颜色
                  lineStyle:{
                    color:'#D87A80'  //线的颜色
                  }
                }
              },
              barWidth : 60,//柱图宽度
              data:eleStopNumArr
            }
          ]
        };
      }
    });
  }

















}
